//
//  HHEnterCodeViewController.m
//  HHBroker
//
//  Created by He Wei on 10/1/15.
//  Copyright © 2015 harry. All rights reserved.
//

#import "HHEnterCodeViewController.h"
#import "HHTimerButton.h"
#import "HttpRequestManager.h"
#import "NSString+ValidateArgument.h"
#import "JiaYuanEncrypt.h"

@interface HHEnterCodeViewController ()

@property (weak, nonatomic) IBOutlet HHTimerButton *resentButton;

@property (nonatomic, weak) IBOutlet UITextField *authorCodeTextField;
@property (nonatomic, weak) IBOutlet UIButton *nextStepButton;
@property (nonatomic, weak) IBOutlet UILabel *teleNumLabel;

@end

@implementation HHEnterCodeViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"更换绑定手机";
    
    NSString *phone = [self.readAuthorCodePhone stringByReplacingCharactersInRange:NSMakeRange(4, 4) withString:@"****"];
    self.teleNumLabel.text = phone;
   [self.resentButton addTarget:self action:@selector(resentButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
}




- (void)resentButtonClicked:(HHTimerButton *)button
{
    
    NSString *usePhone = self.readAuthorCodePhone;
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hub.mode = MBProgressHUDModeIndeterminate;
    hub.labelText = @"正在获取验证码";
    button.timeOut = 60;
    return;
    NSString *urlString = [NSString stringWithFormat:@"%@&mobile=%@", KUrlChangePhoneGetCode, usePhone];
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {
            button.timeOut = 60;

            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_success"]];
            hub.mode = MBProgressHUDModeCustomView;
            hub.customView = imageView;
            hub.labelText = @"发送成功";
            hub.completionBlock = ^ {
                [self.authorCodeTextField becomeFirstResponder];
            };
            [hub hide:YES afterDelay:2];
        } else {
            hub.mode = MBProgressHUDModeText;
            hub.labelText = [response objectForKey:@"msg"];
            [hub hide:YES afterDelay:2];
        }
        
    } failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"网络有点问题";
        [hub hide:YES afterDelay:2];
        
    } isLogin:YES];

}

- (IBAction)nextStepButtonClicked:(UIButton *)sender
{
    [self.view endEditing:YES];
    
    NSString *author = self.authorCodeTextField.text;
    author = [author stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *newPhone = self.readAuthorCodePhone;
    if (![NSString validateAuthorCode:author]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入正确的验证码" delegate: nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        return;
    }
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hub.mode = MBProgressHUDModeIndeterminate;
    NSString *urlString = [NSString stringWithFormat:@"%@?mobile=%@&code=%@", KUrlChangePhoneVerifyCode, newPhone, author];
    [HttpRequestManager getWithUrlString:urlString success:^(id response) {
        NSInteger code = [[response objectForKey:@"code"] integerValue];
        if (code == 1) {

            hub.mode = MBProgressHUDModeText;
        
            hub.labelText = @"修改成功";
            hub.completionBlock = ^ {
                [self.navigationController popToRootViewControllerAnimated:YES];
            

            };
            [hub hide:YES afterDelay:2];
        } else {
            hub.mode = MBProgressHUDModeText;
            hub.labelText = [response objectForKey:@"msg"];
            [hub hide:YES afterDelay:2];
        }
    
    } failure:^(NSError *error) {
        hub.mode = MBProgressHUDModeText;
        hub.labelText = @"网络有点问题";
        [hub hide:YES afterDelay:2];
    
    } isLogin:YES];




}

- (IBAction)textFieldValueChanged:(UITextField *)sender
{
    self.nextStepButton.enabled = self.authorCodeTextField.text.length > 0 ? YES : NO;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
}
@end
